<?php
if(!defined('IN_CAI')) {
	exit('Access Denied');
}
/**
 * 灯谜管理
 *
 */
$ac = $_REQUEST['ac'];
$url = '/admin/index.php?do=guess';
if (empty($ac)) {
	$ac = 'list';
}
if ($ac == 'list') {
	$page = intval($_GET['page']) >0 ? intval($_GET['page']) : 1;
	$limit = 20;
	$offset = ($page - 1) *$limit;
	$where = 'where 1 ';
	$ifanswer = intval($_GET['ifanswer']);
	$ifcheck = intval($_GET['ifcheck']);
	$keyword = trim($_GET['keyword']);
	if ($ifanswer >-1 && isset($_GET['ifanswer'])) {
		$where .= " and ifanswer = $ifanswer";
		${'ifanswer_'.$ifanswer} = 'selected="selected"';
	}
	if ($ifcheck >-1 && isset($_GET['ifcheck'])) {
		$where .= " and ifcheck = $ifcheck";
		${'ifcheck_'.$ifcheck} = 'selected="selected"';
	}
	if ($keyword) {
		$where .= " and title like '%$keyword%'";
	}
	$count = $db->result($db->query("select count(*) from ".tname('guess')." $where"),0);
	$query = $db->query("select * from ".tname('guess')." $where order by ifanswer desc,id desc limit $offset,$limit");
	$list = $db->fetch_all($query);
	$pagestr = multi($count,$limit,$page,$url);
	include atemplate('do_guess_list');
} elseif ($ac == 'add' || $ac == 'edit') {

	if (empty($_POST['step'])) {
		$id = intval($_GET['id']);
		if ($ac == 'edit' && $id > 0) {
			$query = $db->query("select * from ".tname('guess')." where id = $id and answernum <1");
			$info = $db->fetch_array ($query);
			if (empty($info)) {
				adminmessage('对不起，找不到该灯谜,或该灯谜已经有人回答');
			}elseif ($info['ifanswer']) {
				adminmessage('该灯谜已经被猜中，不能编辑');
			}
		}
		if ($info['ifcheck']) {
			$ifcheck_checked = "checked";
		}
		if ($info['ifsimple']) {
			$ifsimple_checked = "checked";
		}
		include atemplate('do_guess_add');
	}else {
		$id = intval($_POST['id']);
		$title = trim($_POST['title']);
		$answer = trim($_POST['answer']);
		$prompt = trim($_POST['prompt']);

		$data['title'] = $title;
		$data['answer'] = $answer;
		$data['prompt'] = $prompt;
		if(empty($title) || empty($answer)|| empty($prompt)) {
			adminmessage('请完整填写内容');
		}
		$data['ifcheck'] = intval($_POST['ifcheck']);
		$data['ifsimple'] = intval($_POST['ifsimple']);

		if (!$id) {
			inserttable('guess',$data);
			adminmessage('添加灯谜成功',$url);
		}else{
			updatetable('guess',$data,array('id'=>$id));
			adminmessage('修改灯谜成功',$url);
		}
	}
} elseif ($ac == 'del') {
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	} else {
		$db->query('delete from '.tname('guess').' where ifanswer = 0 and answernum < 1 and id='.$id);
		if ($db->affected_rows()) {
			adminmessage('删除灯谜成功',$url);
		}else {
			adminmessage('删除灯谜失败,请检查该灯谜是否猜中,或被猜过',$url);
		}
	}
} elseif ($ac == 'check') {
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	} else {
		updatetable('guess',array('ifcheck'=>1),array('id'=>$id));
		if ($db->affected_rows()) {
			adminmessage('发布灯谜成功',$url);
		}else {
			adminmessage('发布灯谜失败，请检查是否已经发布成功',$url);
		}
	}
}elseif ($ac == 'uncheck') {
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	} else {
		$db->query('update '.tname('guess')." set ifcheck = 0 where id = $id and answernum < 1");
//		updatetable('guess',array('ifcheck'=>0),array('id'=>$id));
		if ($db->affected_rows()) {
			adminmessage('取消发布灯谜成功',$url);
		}else {
			adminmessage('取消发布灯谜失败',$url);
		}
	}
}
?>